package com.lrkj.arcface.controller;

import com.arcsoft.face.ActiveFileInfo;
import com.arcsoft.face.EngineConfiguration;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FunctionConfiguration;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import com.lrkj.arcface.service.AceFaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;

/**
 * arcface引擎人脸识别Controller
 *
 * @author wyd
 * @date 2022-07-13
 */
@Controller
@RequestMapping("/arcface")
public class ArcFaceController {

    @Autowired
    private AceFaceService aceFaceService;
    public static FaceEngine faceEngine=null;

    //arcface引擎初始化
    //@PostConstruct
    public void initace(){
        //从官网获取
        String appId = "2U6wLFTQSoXLmBRwZAb2kVrt5AWRPzRhTYZbkreZ2u11";
        String sdkKey = "AFkxeXSppXU1GaWJe4G1JNzAkoo7AB36JerT8Sj31zK3";
        faceEngine = new FaceEngine("F:\\01wyd\\02facedis\\ArcSoft_ArcFace_Java_Windows_x64_V3.0\\libs\\WIN64");
        //激活引擎
        int errorCode = faceEngine.activeOnline(appId, sdkKey);

        if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            System.out.println("arcface引擎激活失败");
        }
        ActiveFileInfo activeFileInfo=new ActiveFileInfo();
        errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
        if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            System.out.println("arcface获取激活文件信息失败");
        }
        //引擎配置
        EngineConfiguration engineConfiguration = new EngineConfiguration();
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
        engineConfiguration.setDetectFaceMaxNum(10);
        engineConfiguration.setDetectFaceScaleVal(16);
        //功能配置
        FunctionConfiguration functionConfiguration = new FunctionConfiguration();
        functionConfiguration.setSupportAge(true);
        functionConfiguration.setSupportFace3dAngle(true);
        functionConfiguration.setSupportFaceDetect(true);
        functionConfiguration.setSupportFaceRecognition(true);
        functionConfiguration.setSupportGender(true);
        functionConfiguration.setSupportLiveness(true);
        functionConfiguration.setSupportIRLiveness(true);
        engineConfiguration.setFunctionConfiguration(functionConfiguration);
        //初始化引擎
        errorCode = faceEngine.init(engineConfiguration);
        if (errorCode != ErrorInfo.MOK.getValue()) {
            System.out.println("arcface初始化引擎失败");
        }else if(errorCode == ErrorInfo.MOK.getValue()) {
            System.out.println("arcface初始化引擎成功");
        }

    }

    //arcface模式接口
    @RequestMapping(value = "/faceimg", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String getPicture(String  base64img){
        return  aceFaceService.faceDisplayImg(base64img);
    }
}
